package demo02;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.regex.Pattern;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Matcher;

public class nameStringdemo {		
	public static void main(String[] args) throws Exception {
        String melx = "https://www.yw11.com/baijiaxing/2020/0515/37774.html";
        String melm = "http://mingzi.jb51.net/qiming/nanhai/68814860866.html";
        String fmelm = "http://mingzi.jb51.net/qiming/nvhai/68815554142.html";
        String strXing = webCrawler(melx);
        String strMing = webCrawler(melm);
        String fMing = webCrawler(fmelm);
        List<String> xingList = getData(strXing, "(.、){3}.", 0);
        List<String> mingList = getData(strMing, "(..、){4}..", 0);
        List<String> fmingList = getData(fMing, "(..、){4}..", 0);

        List<String> fistname = xingList.stream()
                .flatMap(s -> Arrays.stream(s.split("、")))
                .collect(Collectors.toList());

        List<String> nameList = mingList.stream()
                .flatMap(s -> Arrays.stream(s.split("、")))
                .collect(Collectors.toList());
        List<String> fnameList = fmingList.stream()
                .flatMap(s -> Arrays.stream(s.split("、")))
                .collect(Collectors.toList());

        List<String> infos = getInfos(fistname, nameList, 50);
        List<String> finfos = getInfos(fistname, fnameList, 50);

        BufferedWriter bw = new BufferedWriter(new FileWriter("names.txt"));
        Random random = new Random();
        for (String str : infos) {
            int age = random.nextInt(10) + 18;
            bw.write(str + "—男-" + age);
            bw.newLine();
        }
        for (String str : finfos) {
            int age = random.nextInt(8) + 18;
            bw.write(str + "—女-" + age);
            bw.newLine();
        }
        bw.close();
    }

    public static List<String> getInfos(List<String> familyNameList, List<String> nameList, int num) {
        HashSet<String> set = new HashSet<>();
        while (true) {
            if (set.size() == num) {
                break;
            }
            Collections.shuffle(familyNameList);
            Collections.shuffle(nameList);
            set.add(familyNameList.get(0) + nameList.get(0));
        }
        ArrayList<String> xinList = new ArrayList<>(set);
        return xinList;
    }

    private static List<String> getData(String strXing, String regex, int index) {
        List<String> list = new ArrayList<>();
        Pattern pattern = Pattern.compile(regex);
        java.util.regex.Matcher matcher = pattern.matcher(strXing);
        while (matcher.find()) {
            String group = matcher.group(index);
            list.add(group);
        }
        return list;
    }

    public static String webCrawler(String net) throws Exception {
        StringBuilder sb = new StringBuilder();
        URL url = new URL(net);
        URLConnection openConnection = url.openConnection();
        InputStream inputStream = openConnection.getInputStream();
        InputStreamReader isr = new InputStreamReader(inputStream, "utf-8");
        int ch;
        while ((ch = isr.read()) != -1) {
            sb.append((char) ch);
        }
        isr.close();
        return sb.toString();
    }
}